package com.ruoyi.project.crm.controller;

import java.util.List;

import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.crm.domain.Linkman;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.crm.domain.Linkman;
import com.ruoyi.project.crm.service.ILinkmanService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import javax.servlet.http.HttpServletRequest;

/**
 * 联系人Controller
 * 
 * @author ruoyi
 * @date 2020-06-18
 */
@RestController
@RequestMapping("/crm/linkman")
public class LinkmanController extends BaseController
{
    @Autowired
    private ILinkmanService linkmanService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询联系人列表
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:list')")
    @GetMapping("/list")
    public TableDataInfo list(Linkman linkman,HttpServletRequest request)
    {
        startPage();
        Linkman _linkman = linkman;
        LoginUser loginUser = tokenService.getLoginUser(request);
        _linkman.setCreateUserid(loginUser.getUser().getUserId());
        //List<Linkman> list = linkmanService.selectLinkmanList(linkman);
        List<Linkman> list = linkmanService.selectLinkmanList(_linkman);
        return getDataTable(list);
    }

    /**
     * 导出联系人列表
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:export')")
    @Log(title = "联系人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Linkman linkman)
    {
        List<Linkman> list = linkmanService.selectLinkmanList(linkman);
        ExcelUtil<Linkman> util = new ExcelUtil<Linkman>(Linkman.class);
        return util.exportExcel(list, "linkman");
    }

    /**
     * 获取联系人详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:query')")
    @GetMapping(value = "/{linkmanId}")
    public AjaxResult getInfo(@PathVariable("linkmanId") Long linkmanId)
    {
        return AjaxResult.success(linkmanService.selectLinkmanById(linkmanId));
    }

    /**
     * 新增联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:add')")
    @Log(title = "联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Linkman linkman,HttpServletRequest request)
    {
        Linkman _linkman = linkman;
        LoginUser loginUser = tokenService.getLoginUser(request);
        _linkman.setCreateUserid((long)loginUser.getUser().getUserId());
        return toAjax(linkmanService.insertLinkman(_linkman));
    }

    /**
     * 修改联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:edit')")
    @Log(title = "联系人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Linkman linkman) {
        return toAjax(linkmanService.updateLinkman(linkman));
    }

    /**
     * 删除联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:linkman:remove')")
    @Log(title = "联系人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{linkmanIds}")
    public AjaxResult remove(@PathVariable Long[] linkmanIds)
    {
        return toAjax(linkmanService.deleteLinkmanByIds(linkmanIds));
    }
}
